// https://leetcode-cn.com/problems/restore-ip-addresses/
// 93. 复原 IP 地址

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

// struct TreeNode {
//     int val;
//     TreeNode *left;
//     TreeNode *right;
//     TreeNode() : val(0), left(nullptr), right(nullptr) {}
//     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
// };



class Solution {
public:
    vector<string> restoreIpAddresses(string s) {
        backtrack(s, 0, 0);
        return result;
    }
private:
    vector<string> result;

    void backtrack(string& s, int start_index, int num_point) {
        if (num_point == 3) {
            if (IsIP(s, start_index, s.size() - 1)) {
                result.push_back(s);
            }
            return;
        }

        for (int i = start_index; i < s.size(); i++) {
            if (IsIP(s, start_index, i)) {
                s.insert(s.begin() + i + 1, '.');
                backtrack(s, i + 2, num_point + 1);
                s.erase(s.begin() + i + 1);
            } else {
                break;
            }
        }
    }

    bool IsIP(const string s, int start, int end) {
        if (start > end)
            return false;
        if (s[start] == '0' && start != end) 
            return false;
        int num = 0;
        for (int i = start; i <= end; i++) {
            if (s[i] > '9' || s[i] < '0')
                return false;
            num = num * 10 + (s[i] - '0');
            if (num > 255)
                return false;
        }
        return true;
    }
};



int main() {
    Solution obj = Solution();
}